package com.ruoyi.fly.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.AppletMember;
import com.ruoyi.fly.domain.AppletWithdraw;
import com.ruoyi.fly.domain.CeresMember;
import com.ruoyi.fly.domain.vo.AppletWithdrawSearchVo;
import com.ruoyi.fly.domain.vo.AppletWithdrawUpdateVo;
import com.ruoyi.fly.mapper.AppletMemberMapper;
import com.ruoyi.fly.mapper.AppletWithdrawMapper;
import com.ruoyi.fly.service.AppletWithdrawService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.List;

/**
 * @Author: wdf
 * @Date: 2024/6/15
 */
@Service
public class AppletWithdrawServiceImpl implements AppletWithdrawService {

    @Autowired
    private AppletWithdrawMapper appletWithdrawMapper;
    @Autowired
    private AppletMemberMapper appletMemberMapper;


    /**
     * 查询提现列表
     *
     * @param appletWithdrawSearchVo 提现
     * @return
     */
    @Override
    public R selectAppletWithdrawList(AppletWithdrawSearchVo appletWithdrawSearchVo) {
        PageHelper.startPage(appletWithdrawSearchVo.getPageNum(), appletWithdrawSearchVo.getPageSize());
        List<AppletWithdraw> appletWithdraws = appletWithdrawMapper.selectAppletWithdrawList2(appletWithdrawSearchVo);
//        for (AppletWithdraw appletWithdraw : appletWithdraws) {
//            CeresMember ceresMember = appletMemberMapper.selectAppletMemberById(String.valueOf(appletWithdraw.getMid()));
//            if (ceresMember != null){
//                appletWithdraw.setMid(Long.valueOf(ceresMember.getNickname()));
//            }
//        }

        PageInfo<AppletWithdraw> pageInfo = new PageInfo<>(appletWithdraws);
        return R.ok(pageInfo);
    }

    /**
     * 提现审核
     *
     * @param appletWithdrawUpdateVo 提现
     * @return
     */
    @Override
    public R updateAppletWithdraw(AppletWithdrawUpdateVo appletWithdrawUpdateVo) {
        AppletWithdraw withdraw = appletWithdrawMapper.selectAppletWithdrawById(appletWithdrawUpdateVo.getId());
        if (withdraw.getApplyStatus()!=0){
            return R.fail("不能重复审核!!!");
        }
        appletWithdrawUpdateVo.setUpdateUser(String.valueOf(SecurityUtils.getUserId()));
        if (appletWithdrawUpdateVo.getApplyStatus() == 1){
            CeresMember ceresMember = appletMemberMapper.selectAppletMemberById(String.valueOf(appletWithdrawUpdateVo.getMid()));
            AppletWithdraw appletWithdraw = appletWithdrawMapper.selectAppletWithdrawById(appletWithdrawUpdateVo.getId());
            ceresMember.setBalance(ceresMember.getBalance().subtract(appletWithdraw.getCash()));
            AppletMember appletMember = new AppletMember();
            BeanUtils.copyProperties(ceresMember,appletMember);
            appletMemberMapper.updateAppletMember(appletMember);
            appletWithdrawMapper.updateStatusByOrderCode(withdraw.getOrderCode());
        }

        return appletWithdrawMapper.updateAppletWithdraw(appletWithdrawUpdateVo) > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }
}
